const app = getApp()
import { requestApi } from '../../../api'
import { formatImage } from '../../../utils/formatImage'
import { formatPrice } from '../../../utils/util'
var util = require('../../../utils/util.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    loadingHidden:false,
    activityList:[],
    start: 0,
    count: 20,
    winHeight: 0,
    channel: '',
    noMore: false,
    requestFlag: false,         // 加载瀑布流的请求标识
    scrollTop: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let channel = options && options.channel || 'wx_xcx'

    this.data.channel = channel
    this.getActivityList(this.data.start, channel)

    let systemInfo = {}

    try {
      systemInfo = wx.getSystemInfoSync()
    } catch (e) {
      // Do something when catch error
    }

    this.setData({
      winWidth: systemInfo.windowWidth,
      winHeight: systemInfo.windowHeight,
    })
  },

  scroll: function(event) {
    this.setData({
      scrollTop: event.detail.scrollTop
    });
  }, 

  refresh: function(e) {
    this.setData({
      scrollTop: 0,
    })

    this.refreshActivityList(0, this.data.channel)
  },

  getActivityList: function(start, channel) {
    if (this.data.noMore) return false

    if (this.data.requestFlag) return false

    this.setData({
      requestFlag: true,
    })

    let noMore = false
    let that = this

     app.request({
        url: requestApi.activityListUrl,
	      login: false,
        data: {
          start: start,
          count: that.data.count,
          channel: channel,
        },
      }, (data) => {

        if (data.original.data.length < that.data.count) {
          noMore = true
        }

        data.original.data.forEach((item) => {
          //限时折扣
          if (item.topics_type == 15) {
            item.activity_img.new_path = formatImage.cdnImage(item.activity_img.path, formatImage.THUMBNAIL_M)
            item.activity_status_cn = that.getActivityStatusCn(item.topics_type, item.activity_status)

            if (item.activity_status == 1) {//未开始
              item.desc = "活动即将开始"
            }
            else if (item.activity_status == 2){//进行中
              item.btn_enable = true
              item.desc = "距本次限时活动结束还有 "
              item.leftTime = that.getRemainTime(item.left_time)
            }
            else if (item.activity_status == 3) {//结束
              item.desc = "活动已结束"
            }
          } else if (item.topics_type == 16) {
            item.activity_img.new_path = formatImage.cdnImage(item.activity_img.path, formatImage.THUMBNAIL_M)
            item.activity_status_cn = that.getActivityStatusCn(item.topics_type, item.activity_status)

            if (item.activity_status == 1) {//未开始
              item.desc = "拼团即将开始"
            }
            else if (item.activity_status == 2) {//进行中
              item.btn_enable = true
              item.desc = "距本拼团结束还有 "
              item.leftTime = that.getRemainTime(item.left_time)
            }
            else if (item.activity_status == 3) {//结束
              item.desc =  "拼团已结束"
            }
          } else if (item.topics_type == 0) {
            item.product_cover_image.new_path = formatImage.cdnImage(item.product_cover_image.path, formatImage.THUMBNAIL_S)
            item.product_cover_image.padding_bottom = item.product_cover_image.height / item.product_cover_image.width * 100
            item.new_goods_price = formatPrice(item.goods_price / 100)
            item.new_activity_price = formatPrice(item.activity_price / 100)
          }
        })

        that.setData ({
          activityList: that.data.activityList.concat(data.original.data),
          start: that.data.start+that.data.count,
          loadingHidden: true,
          noMore: noMore,
        })
      }, (error) => {
        wx.showModal({
          title: '提示',
          showCancel: false,
          content: error,
        })
      }, () => {
        that.setData({
          requestFlag: false,
        })
      })
  },

  refreshActivityList: function(start, channel) {
    if (this.data.requestFlag) return false

    this.setData({
      requestFlag: true,
    })

    let noMore = false

    app.request({
      url: requestApi.activityListUrl,
      login: false,
      data: {
        start: start,
        count: this.data.count,
        channel: channel,
      },
    }, (data) => {
      data.original.data.forEach((item) => {
        //限时折扣
        if (item.topics_type == 15) {
          item.activity_img.new_path = formatImage.cdnImage(item.activity_img.path, formatImage.THUMBNAIL_M)
          item.activity_status_cn = this.getActivityStatusCn(item.topics_type, item.activity_status)

          if (item.activity_status == 1) {//未开始
            item.desc = "活动即将开始"
          }
          else if (item.activity_status == 2) {//进行中
            item.btn_enable = true
            item.desc = "距本次限时活动结束还有 "
            item.leftTime = this.getRemainTime(item.left_time)
          }
          else if (item.activity_status == 3) {//结束
            item.desc = "活动已结束"
          }

        } else if (item.topics_type == 16) {
          item.activity_img.new_path = formatImage.cdnImage(item.activity_img.path, formatImage.THUMBNAIL_M)
          item.activity_status_cn = this.getActivityStatusCn(item.topics_type, item.activity_status)

          if (item.activity_status == 1) {//未开始
            item.desc = "拼团即将开始"
          }
          else if (item.activity_status == 2) {//进行中
            item.btn_enable = true
            item.desc = "距本拼团结束还有 "
            item.leftTime = this.getRemainTime(item.left_time)
          }
          else if (item.activity_status == 3) {//结束
            item.desc = "拼团已结束"
          }
        } else if (item.topics_type == 0) {
          item.product_cover_image.new_path = formatImage.cdnImage(item.product_cover_image.path, formatImage.THUMBNAIL_S)
          item.product_cover_image.padding_bottom = item.product_cover_image.height / item.product_cover_image.width * 100
          item.new_goods_price = formatPrice(item.goods_price / 100)
          item.new_activity_price = formatPrice(item.activity_price / 100)
        }
      })

      this.setData({
        activityList: data.original.data,
        start: this.data.count,
        loadingHidden: true,
        noMore: false,
      })
    }, (error) => {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: error,
      })
    }, () => {
      this.setData({
        requestFlag: false,
      })
    })

  },

  addActivityList: function(e) {
    this.getActivityList(this.data.start, this.data.channel)
  },

  didClickAcitvityItem: function (e) {
    let tId = e.currentTarget.id
    let url = '../activityDetail/activityDetail?tId=' + tId + '&channel=' + this.data.channel
    wx.navigateTo({
      url: url,
    })
  },

  didClickGoodsItem: function (e) {
    let gid = e.currentTarget.id
    let url = '../../goods/goodsDetail/goodsDetail?gid=' + gid + '&channel=' + this.data.channel
    wx.navigateTo({
      url: url,
    })
  },

  // 时间格式化
  getRemainTime: function (time) {
    if (time < 0) return false

    let differTimestamp = time
    let deltaHour = Math.floor(differTimestamp / (60 * 60))
    let deltaMinute = Math.floor((differTimestamp % (60 * 60)) / 60)
    let deltaSecond = Math.floor(differTimestamp % 60)
    deltaHour = deltaHour < 10 ? ('0' + deltaHour) : deltaHour
    deltaMinute = deltaMinute < 10 ? ('0' + deltaMinute) : deltaMinute
    deltaSecond = deltaSecond < 10 ? ('0' + deltaSecond) : deltaSecond
    let deltaDate = deltaHour + ' : ' + deltaMinute + ' : ' + deltaSecond

    return deltaDate
  },

  /**
   * 拼团卡片的按钮文案
   */
  getActivityStatusCn: function(activityType, activityStatus) {
    if (activityType == 15) {
      switch (activityStatus) {
        case 1:
        case 2:
          return '限时活动';
        case 3:
          return '活动结束';
        default:
          return '-';
      }
    } else if (activityType == 16) {
      switch (activityStatus) {
        case 1:
          return '即将开团';
        case 2:
          return '立即拼团';
        case 3:
          return '拼团结束';
        default:
          return '';
      }
    }
  }
})
